package lambada表达式;

import lambada表达式.entity.Student;

import java.math.BigDecimal;
import java.util.function.*;

/*函数式接口*/
public class FunctionalInterfaceTest {

    public static void main(String[] args) {
        Student student = new Student("张三", 23, 170);
        /*Predicate*/
        Predicate<Integer> predicate = x -> x > 180;
        System.out.println("predicate的使用：" + predicate.test(student.getStature()));
        /*Consumer*/
        Consumer<String> consumer = System.out::println;
        consumer.accept("cosumer的使用");
        /*Function*/
        Function<Student, String> function = Student::getName;
        String name = function.apply(student);
        System.out.println("function的使用：" + name);
        /*Supplier*/
        Supplier<Integer> supplier = () -> Integer.valueOf(BigDecimal.TEN.toString());
        System.out.println("supplier的使用：" + supplier.get());
        /*UnaryOperator，入参、出参同类型的特殊function接口*/
        UnaryOperator<Boolean> unaryOperator = boo -> !boo;
        Boolean boo = unaryOperator.apply(true);
        System.out.println("unaryOperator的使用：" + boo);
        /*BiFunction，两个入参的类似function接口*/
        BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> x * y;
        Integer integer = biFunction.apply(2, 3);
        System.out.println("biFunction的使用：" + integer);
        /*自定义函数式接口*/
        work(new Work() {
            @Override
            public String work() {
                return "自定义函数式接口的使用：哈哈哈";
            }
        });
        work(() -> "自定义函数式接口的使用：嘿嘿嘿");
    }

    static void work(Work work) {
        String str = work.work();
        System.out.println(str);
    }

}

@FunctionalInterface //加上该注解声明为函数式接口，只能有一个未实现的方法，否则报错；
interface Work {
    String work();
}

